import {OG} from "./CommonClass";
import { CommonFunction } from "./CommonFunction";
/**Canvas绘制类 */
export class DrawClass {
    private static self:DrawClass;
    /**当前要绘制的Canvas*/
    public static drawCanvas:HTMLCanvasElement;
    /**当前上下文内容 */
    private Ctx:CanvasRenderingContext2D = null;
    /**旋转角度(弧度制)*/
    private Angle:number = 0; 
    
    public static ins() {
        if(this.self != null) {
            return DrawClass.self;
        }else {
            return new DrawClass();
        }
    }

    public constructor () {
        DrawClass.self = this;
    }

    /**绑定当前要绘制的canvas 
     * @param cvs 需要绘制的canvas标签
    */
    public BindCanvas (cvs:HTMLCanvasElement) {
        if(cvs != null) {
            DrawClass.drawCanvas = cvs;
            this.Ctx = cvs.getContext("2d");
        }
    }

    /**绑定当前绘制的上下文 */
    public BindContext (ctx:any) {
        if(ctx != null) {
            this.Ctx = ctx;
        }
    }

    /**返回当前绘制Canvas的上下文 */
    public ReturnContext () {
        return this.Ctx;
    }

    /**清除整个画布 
     * @param area 清空区域
     * @param ctx 画布上下文
    */
    public ClearAllCanvas (area:OG.Size,ctx:any) {
        if(ctx != null) {
            ctx.clearRect(0,0,area.width,area.height);
        }
    }

    /** 绘制虚线
     * @param interval 虚线间的间隔
     * @param instance 每段虚线的长度
     * @param startPos 开始绘制位置
     * @param endPos 结束绘制位置
    */
    public DottedLine (interval: number,instance:number,startPos:OG.Positon,endPos:OG.Positon) {
        let state:string = "line";  //绘制形态
        let sum:number = 0; //虚线绘制个数
        let stop_pos:OG.Positon = new OG.Positon();
        let move_pos:OG.Positon = new OG.Positon();
        stop_pos.x = startPos.x;
        stop_pos.y = startPos.y;
        move_pos.x = stop_pos.x;
        move_pos.y = stop_pos.y;
        if(this.IsDraw() == false) {
            return;
        }
        if(startPos.x == endPos.x && startPos.y != endPos.y) {
            state = "v-line";  //竖线
        }else if(startPos.y == endPos.y && startPos.x != endPos.x) {
            state = "h-line";  //横线
        }else {
            state = "o-line";  //斜线
        }
        //绘制虚线的段数
        sum = CommonFunction.ins().InstanceToPoints(startPos,endPos) / instance;
        sum = parseInt(sum.toFixed(0));
        this.Ctx.beginPath();
        for(let i = 0; i < sum; i++) {
            switch(state) {
                case "h-line":
                    this.Ctx.moveTo(stop_pos.x,stop_pos.y);
                    move_pos.x = (stop_pos.x + instance);
                    stop_pos.x += (instance + interval);
                break;
                case "v-line":
                    this.Ctx.moveTo(stop_pos.x,stop_pos.y);
                    move_pos.y = (stop_pos.y + instance);
                    stop_pos.y += (instance + interval);
                break;
                case "o-line":
                    this.Ctx.moveTo(stop_pos.x,stop_pos.y);
                    move_pos.x = (stop_pos.x + instance);
                    move_pos.y = (stop_pos.y + instance);
                    stop_pos.x += (interval + instance);
                    stop_pos.y += (interval + instance);
                break;
            }
            this.Ctx.lineTo(move_pos.x,move_pos.y);
            
        }
        this.Ctx.stroke();
        this.Ctx.closePath();
    }

    /**绘制图片
     * @param m_img 图片源
     * @param m_slice 图片裁剪位置
     * @param m_size 图片绘制大小
     * @param m_pos 图片放置位置
    */
    public PaintImage (m_img:any,m_slice:OG.Positon,m_size:OG.Size,m_pos:OG.Positon) {
        if(this.IsDraw() == false) {
            return;
        }
        if(this.Angle != 0) {
            this.Ctx.save();  //记录当前绘图状态
            //更改中心点
            this.Ctx.translate(m_pos.x + (m_size.width / 2),m_pos.y + (m_size.height / 2));
            this.Ctx.rotate(this.Angle);  //旋转图块
            this.Ctx.drawImage(m_img,m_slice.x,m_slice.y,
                m_size.width,m_size.height,-(m_size.width / 2),-(m_size.height / 2),m_size.width,m_size.height);
            this.Ctx.restore();  //恢复上次绘图状态
        }else {
            this.Ctx.drawImage(m_img,m_slice.x,m_slice.y,
                m_size.width,m_size.height,m_pos.x,m_pos.y,m_size.width,m_size.height);
        }
    }

    /**
     * 顺时针旋转图片
     * @param m_angle 旋转角度(角度制) 默认一次旋转90度 
     */
    public RotateImage (m_angle:number = 90) {
        //转换为弧度制
        let radius:number = m_angle * (Math.PI / 180);
        this.Angle = radius;
    }

    /**清除图片 
    *@param m_pos 图片放置位置
    * @param m_size 图片绘制大小
    */
    public DeleteImage(m_pos:OG.Positon,m_size:OG.Size) {
        if(this.IsDraw() == false) {
            return;
        }
        this.Ctx.clearRect(m_pos.x,m_pos.y,m_size.width,m_size.height);
    }

    /**绘制矩形 
     * @param lt_pos 左上角坐标
     * @param rt_size 矩形宽高
     * @param is_fill 是否填充
    */
    public DrawRect (lt_pos:OG.Positon,rt_size:OG.Size,is_fill:boolean) {
        if(this.IsDraw() == false) {
            return;
        }
        if(is_fill == true) {
            this.Ctx.fillRect(lt_pos.x,lt_pos.y,rt_size.width,rt_size.height);
        }else {
            this.Ctx.rect(lt_pos.x,lt_pos.y,rt_size.width,rt_size.height);
        }
    }

    /**Canvas绑定判断 */
    private IsBindCanvas () {
        if(DrawClass.drawCanvas == null) {
            alert("未绑定Canvas标签!");
            return false;
        }
        return true;
    }

    /**Context上下文获取判断 */
    private IsGetContext () {
        if(this.Ctx == null) {
            alert("未获取Context上下文!");
            return false;
        }
        return true;
    }

    /**绘制前提判断*/
    private IsDraw () {
        if(this.IsBindCanvas() == false || this.IsGetContext() == false) {
            return false;
        }
        return true;
    }
}